//
//  Course.m
//  Gradekeeper0.2
//
//  Created by Qian Tim on 09-11-12.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "Courses.h"

@implementation Courses

@synthesize weightOfWorks;
@synthesize workTypes;
@synthesize CourseWorks;
@synthesize catagory;
@synthesize number;
@synthesize grade;
@synthesize gradePoint;
@synthesize credit;
@synthesize types;
@synthesize weights;

-(Courses*) initWithCatagory: (NSString*) newCatagory number: (int) newNumber credit: (int) newCredit types: (NSString*) newTypes weights: (NSString*) newWeights {
	self = [super init];
	
    if ( self ) {
        workTypes = [[NSMutableArray alloc] initWithCapacity:3];
		weightOfWorks = [[NSMutableArray alloc] initWithCapacity:3];
		CourseWorks = [[NSMutableArray alloc] initWithCapacity:5];
		self.catagory = newCatagory;
		self.number = newNumber;
		self.credit = newCredit;
		self.types = newTypes;
		self.weights = newWeights;
		NSArray *type = [newTypes componentsSeparatedByString:@"/"];
		for (int i=0; i<[type count]; i++) {
			[workTypes addObject:[type objectAtIndex:i]];
		}
		NSArray *weight = [newWeights componentsSeparatedByString:@"/"];
		for (int i=0; i<[weight count]; i++) {
			[weightOfWorks addObject:[weight objectAtIndex:i]];
		}
    }
    return self;
}

//the datastructure for Courses is alphabetically sorted by insertion sort, and auto-sorts upon item entry.
-(BOOL) addWork: (CourseWork*) work {
	NSEnumerator *enumerator = [self.CourseWorks objectEnumerator];
    id obj;
	NSUInteger i = 0;
	
    while ( obj = [enumerator nextObject] ) {
        if ([obj name] > [work name]) {
			[self.CourseWorks insertObject:work atIndex:i];
			return YES;
		} else if ([obj name] == [work name]) {
			return NO;
		}
		i++;
    }
	
	[self.CourseWorks addObject:work];
	return YES;
}

-(NSString*) getName {
	return [NSString stringWithFormat:@"%@ %i", self.catagory , self.number] ;
}
		 
-(void) calcGrade {
	NSMutableArray *workTypedCollections = [NSMutableArray arrayWithCapacity:[workTypes count]];
	NSUInteger i;
	for (i=0; i<[self.workTypes count]; i++) {
		NSEnumerator *enumerator = [self.CourseWorks objectEnumerator];
		id obj;
		NSMutableArray *workCollection = [NSMutableArray arrayWithCapacity:1];
		
		while ( obj = [enumerator nextObject] ) {
			if ([[obj type] compare:[self.workTypes objectAtIndex:i]] == NSOrderedSame) {
				[workCollection addObject:obj];
			}
		}
		[workTypedCollections addObject:workCollection];
	}
	
	float currGrade = 0;
	for (i=0; i<[workTypedCollections count]; i++) {
		float typeAvg = 0;
		NSEnumerator *enumerator = [[workTypedCollections objectAtIndex:i] objectEnumerator];
		id obj;
		
		while (obj = [enumerator nextObject]) {
			typeAvg += [obj percentage];
		}
		typeAvg = typeAvg/[[workTypedCollections objectAtIndex:i] count];
		currGrade += typeAvg * [[self.weightOfWorks objectAtIndex:i] intValue]/100;
	}
	[self setGrade:currGrade];
}

-(void) dealloc {
    [super dealloc];
	[workTypes dealloc];
	[weightOfWorks dealloc];
	[CourseWorks dealloc];
}


@end
